﻿using DatabaseBackup.Models;
using DatabaseBackup.Services;
using Newtonsoft.Json;


namespace DatabaseBackup
{
    class Program
    {
        private const string ConfigFileName = "appsettings.json";

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("数据库自动备份程序启动...");

                // 加载配置文件
                AppConfig config = LoadConfig();
                if (config == null)
                {
                    Console.WriteLine("无法加载配置文件，程序将退出。");
                    return;
                }

                // 创建备份调度器并启动
                var scheduler = new BackupScheduler(config);
                scheduler.Start();

                Console.WriteLine("按 'q' 键退出程序...");
                while (Console.ReadKey(true).Key != ConsoleKey.Q)
                {
                    // 等待用户按Q退出
                }

                scheduler.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"程序出错: {ex.Message}");
                Console.WriteLine("按任意键退出...");
                Console.ReadKey();
            }
        }

        /// <summary>
        /// 加载配置文件
        /// </summary>
        private static AppConfig LoadConfig()
        {
            try
            {
                if (!File.Exists(ConfigFileName))
                {
                    // 如果配置文件不存在，创建一个默认配置文件
                    var defaultConfig = new AppConfig
                    {
                        BackupHour = 2,
                        BackupMinute = 0,
                        BackupPath = "Backups",
                        RetainDays = 7,
                        MySqlDatabases = new System.Collections.Generic.List<MySqlDatabaseConfig>
                        {
                            new MySqlDatabaseConfig
                            {
                                DatabaseName = "your_mysql_db",
                                Server = "localhost",
                                Port = 3306,
                                Username = "root",
                                Password = "password",
                                Enabled = false
                            }
                        },
                        SqlServerDatabases = new System.Collections.Generic.List<SqlServerDatabaseConfig>
                        {
                            new SqlServerDatabaseConfig
                            {
                                DatabaseName = "your_sqlserver_db",
                                Server = "localhost",
                                UseWindowsAuth = true,
                                Enabled = false
                            }
                        }
                    };

                    string defaultConfigJson = JsonConvert.SerializeObject(defaultConfig, Formatting.Indented);
                    File.WriteAllText(ConfigFileName, defaultConfigJson);

                    Console.WriteLine($"已创建默认配置文件: {ConfigFileName}");
                    Console.WriteLine("请编辑配置文件后重新启动程序。");
                    return null;
                }

                // 读取并解析配置文件
                string configJson = File.ReadAllText(ConfigFileName);
                return JsonConvert.DeserializeObject<AppConfig>(configJson);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"加载配置文件出错: {ex.Message}");
                return null;
            }
        }
    }
}
